# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.exceptions import UserError

CATEGORY_SELECTION = [
    ('required', '必须'),
    ('optional', '可选'),
    ('no', '无')]


class ApprovalCategory(models.Model):
    _inherit = 'approval.category'

    has_car_no = fields.Selection(CATEGORY_SELECTION, string="车牌号码", default="no", required=True)
    has_carrier = fields.Selection(CATEGORY_SELECTION, string="携带人", default="no", required=True)
    has_carrier_date = fields.Selection(CATEGORY_SELECTION, string="放行日期", default="no", required=True)
    has_carrier_reason = fields.Selection(CATEGORY_SELECTION, string="携带事由", default="no", required=True)
    has_carrier_state = fields.Selection(CATEGORY_SELECTION, string="需放行状态", default="no", required=True)


class ApprovalRequest(models.Model):
    _inherit = 'approval.request'
    _order = 'id desc'
    car_no = fields.Char(string=u'车牌号码')
    carrier = fields.Char(string=u'携带人')
    carrier_date = fields.Datetime(string=u'放行日期')
    carrier_reason = fields.Char(string='携带事由')
    user_ids = fields.Many2many('res.users', related='category_id.user_ids')
    to_user_id=fields.Many2one('res.users',copy=False)
    note=fields.Html(string=u'说明')

    def action_approve(self):
        super(ApprovalRequest, self).action_approve()
        self.to_user_id=self.env.user.id
    carrier_state = fields.Selection([
        ('0', '未放行'),
        ('1', '已放行')
    ],default='0')
    def action_carrier(self):
        self.carrier_state='1'
        self.carrier_date=fields.Datetime.now()
    own_state = fields.Char(string=' ', compute='compute_own_state')

    def compute_own_state(self):
        for r in self:
            if r.request_status == 'approved' and r.category_id.has_carrier_state != 'no':
                if r.carrier_state == '0':
                    r.own_state = '未放行'
                elif r.carrier_state=='1':
                    r.own_state = '已放行'
                else:
                    r.own_state=''
            else:
                r.own_state = ' '

    def _get_protected_fields(self):
        return [
            'request_owner_id', 'category_id', 'date_start', 'date_end', 'items',
            'quantity', 'amount', 'reference', 'location', 'parent_id'
        ]

    has_car_no = fields.Selection(related="category_id.has_car_no")
    has_carrier = fields.Selection(related="category_id.has_carrier")
    has_carrier_date = fields.Selection(related="category_id.has_carrier_date")
    has_carrier_reason = fields.Selection(related="category_id.has_carrier_reason")
    has_carrier_state=fields.Selection(related='category_id.has_carrier_state')

    can_modify = fields.Boolean(compute='compute_can_modify')

    def compute_can_modify(self):

        for r in self:

            if (r.create_uid == self.env.user or self.env.user.has_group(
                    'approvals.group_approval_manager')) and r.request_status in ('new', 'pending'):
                r.can_modify = True
            else:
                r.can_modify = True

    def write(self, vals):
        protected_fields = self._get_protected_fields()
        if self.request_status not in ('new', 'pending') and any(f in vals.keys() for f in protected_fields) and (
                self.create_uid == self.env.user or self.env.user.has_group('approvals.group_approval_manager')):
            raise UserError('你不能修改此单据')
        return super(ApprovalRequest, self).write(vals)
